﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.OleDb;

public partial class ExecuteQuery : System.Web.UI.Page
{

	protected void Page_Load ( object sender, EventArgs e )
	{ }

	protected void cmdExecute_Click ( object sender, EventArgs e )
	{
		OleDbConnection connection = new OleDbConnection ( WebConfigurationManager.ConnectionStrings["SampleConnectionString"].ConnectionString );
		OleDbCommand command = new OleDbCommand ( this.txtSql.Text, connection );
		OleDbDataAdapter adapter = new OleDbDataAdapter ( command );

		this.lblInfo.Text = "...";

		try
		{
			DataSet dataSet = new DataSet ( );

			adapter.Fill ( dataSet );

			foreach ( DataTable table in dataSet.Tables )
			{
				string html = "<table><tr>";

				foreach ( DataColumn column in table.Columns )
					html += string.Format ( "<td><strong>{0}</strong></td>", column.ColumnName );

				html += "</tr>";

				foreach ( DataRow row in table.Rows )
				{
					html += "<tr>";

					foreach ( DataColumn column in table.Columns )
						html += string.Format ( "<td>{0}</td>", row[column] );

					html += "</tr>";
				}

				html += "</table><br /><br />";
				this.lblInfo.Text = html;
			}

		}
		catch ( Exception err )
		{ this.lblInfo.Text = err.Message; }
		finally
		{

			if ( connection.State == ConnectionState.Open )
				connection.Close ( );

		}

	}

}